package it.CookBook;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ShowImage extends Activity {

	ImageView imageView;
	Bitmap bmp;
	boolean showMoreResult = false;
	String imageRsc = null;
	int imagePosition = -1;
	ArrayList<String> listofResults = new ArrayList<String>();
	
	 private GestureDetector mDetector;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image);
		
		Bundle b = getIntent().getExtras();
		imageRsc = b.getString("imageName");
		showMoreResult = b.getBoolean("showMoreResult");
		imagePosition = b.getInt("imagePosition");
		listofResults = b.getStringArrayList("listOfResults");
		if (imagePosition == 0)
			((Button)findViewById(R.id.btnPreviousResult)).setEnabled(false);
		if (listofResults == null || imagePosition + 1 == listofResults.size())
			((Button)findViewById(R.id.btnNextResult)).setEnabled(false);
		showImage(imagePosition);

	}

	private void showImage(int imagePosition){
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inTempStorage = new byte[16 * 1024];
		options.inSampleSize = 2;
		TextView txtImageName = (TextView) findViewById(R.id.txtImageName);
		if (imagePosition == -1){
			bmp = BitmapFactory.decodeFile(imageRsc, options);
			txtImageName.setText(imageRsc.substring(imageRsc.lastIndexOf("/") + 1));
		}
		else{
			bmp = BitmapFactory.decodeFile(listofResults.get(imagePosition), options);
			txtImageName.setText(listofResults.get(imagePosition).substring(listofResults.get(imagePosition).lastIndexOf("/") + 1));
		}
		
		imageView = (ImageView) findViewById(R.id.imageView1);
		imageView.setImageBitmap(bmp);

		if (showMoreResult == false){
			((Button) findViewById(R.id.btnPreviousResult)).setVisibility(View.GONE);
			((Button) findViewById(R.id.btnNextResult)).setVisibility(View.GONE);
		}
		
		imageView.setImageBitmap(bmp);
		
		mDetector = new GestureDetector(this, new MyGestureDetector());
		imageView.setOnTouchListener(new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				if (mDetector.onTouchEvent(event))
					return true;
				
				return true;
			}
		});		
	}
	
	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}


	public void showNextImage(View v){
		++imagePosition;
		((Button)findViewById(R.id.btnPreviousResult)).setEnabled(true);
		if (imagePosition + 1 == listofResults.size()){
			((Button)findViewById(R.id.btnNextResult)).setEnabled(false);
			showImage(imagePosition);
			return;
		}
			
		showImage(imagePosition);
	}

	public void showPreviuosImage(View v){
		--imagePosition;
		((Button)findViewById(R.id.btnNextResult)).setEnabled(true);
		if (imagePosition == 0){
			((Button)findViewById(R.id.btnPreviousResult)).setEnabled(false);
			showImage(imagePosition);
			return;
		}
		showImage(imagePosition);
	}
	
	class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {

		public MyGestureDetector(){
			Log.d("MOUSE", "Costruito");
		}
		
		@Override
		public boolean onDoubleTap(MotionEvent e) {
			Log.d("MOUSE", "onDoubleTap");
			return super.onDoubleTap(e);
		}

		@Override
		public boolean onDoubleTapEvent(MotionEvent e) {
			Log.d("MOUSE", "onDoubleTapEvent");
			return super.onDoubleTapEvent(e);
		}

		@Override
		public boolean onDown(MotionEvent e) {
			Log.d("MOUSE", "onDown");
			return super.onDown(e);
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			Log.d("MOUSE","onFling X="+velocityX+ ",Y="+velocityY);
			if (Math.abs(velocityX) > 3000)
				finish();
			return super.onFling(e1, e2, velocityX, velocityY);
		}

		@Override
		public void onLongPress(MotionEvent e) {
			Log.d("MOUSE","onLongPress");
			super.onLongPress(e);
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			Log.d("MOUSE","onScroll");
			finish();
			return super.onScroll(e1, e2, distanceX, distanceY);
		}

		@Override
		public void onShowPress(MotionEvent e) {
			Log.d("MOUSE","onShowPress");
			super.onShowPress(e);
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			return super.onSingleTapConfirmed(e);
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			Log.d("MOUSE","onSingleTapUp");
			return super.onSingleTapUp(e);
		}
		
	}

}
